<template>
  <div
    :class="className"
    :style="{height: height,width: width}"
  />
</template>

<script>
import echarts from 'echarts' // echarts theme
import { debounce } from '@/utils'
require('echarts/theme/macarons')

export default {
  props: {
    className: {
      type: String,
      default: 'chart'
    },
    width: {
      type: String,
      default: '100%'
    },
    height: {
      type: String,
      default: '300px'
    },
    sumCase: {
      type: Object
    },
    majorCase: {
      type: Object
    },
    parentObject: {
      type: Object
    }
  },
  data() {
    return {
      chart: null,
      parentData: '',
      defaultData: {
        tooltip: {
          trigger: 'item',
          formatter: '{a} <br/>{b} : {c} ({d}%)'
        },
        legend: {
          left: 'center',
          selectedMode: false,
          bottom: '10',
          data: ['Industries', 'Technology', 'Forex', 'Gold', 'Forecasts']
        },
        calculable: true,
        series: [{
          name: 'Default Test Data',
          type: 'pie',
          roseType: 'radius',
          radius: [35, 75],
          center: ['50%', '38%'],
          data: [{
            value: 320,
            name: 'Industries'
          }, {
            value: 240,
            name: 'Technology'
          }, {
            value: 149,
            name: 'Forex'
          }, {
            value: 100,
            name: 'Gold'
          }, {
            value: 59,
            name: 'Forecasts'
          }],
          animationEasing: 'cubicInOut',
          animationDuration: 2600
        }]
      }
    }
  },
  watch: {
    sumCase: {
      deep: true,
      handler(val) {
        this.setOptions(val)
      }
    },
    majorCase: {
      deep: true,
      handler(val) {
        this.setOptions(val)
      }
    },
    parentObject: {
      deep: true,
      handler(val) {
        this.setOptions(val)
      }
    }
  },
  mounted() {
    this.parentData = this.sumCase || this.majorCase || this.parentObject
    this.initChart()
    this.__resizeHanlder = debounce(() => {
      if (this.chart) {
        this.chart.resize()
      }
    }, 100)
    window.addEventListener('resize', this.__resizeHanlder)
  },
  beforeDestroy() {
    if (!this.chart) {
      return
    }
    window.removeEventListener('resize', this.__resizeHanlder)
    this.chart.dispose()
    this.chart = null
  },
  methods: {
    setOptions(obj) {
      // common config
      obj.legend && (obj.legend.selectedMode = false)
      const currColors = location.hash.split('=')[1] && decodeURIComponent(location.hash.split('=')[1]).replace(/\[|\]|\'/img, '').split(',') || ''
      obj.color = currColors || obj.color
      this.chart.setOption(obj)
    },
    initChart() {
      console.log('parentData:', this.parentData)
      this.chart = echarts.init(this.$el, 'macarons')
      if (this.parentData) {
        this.chart.setOption(this.parentData)
        // this.chart.on('finished', arg => {
        /* console.log('chartDataUrl:', this.chart.getDataURL({
                        type: 'jpg',
                        pixelRatio: 1
                    })) */
        // })
        return
      }
      // default test data
      this.chart.setOption(this.defaultData)
    }
  }
}
</script>
